﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using System.Web;
using System.Configuration;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 跟单猿TMS_运单获取
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WaybillInfoToWMSController : ControllerBase
    {
        private readonly ILogger<WaybillInfoToWMSRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_t;


        public WaybillInfoToWMSController(ILogger<WaybillInfoToWMSRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_t = _configuration.GetConnectionString("WMSDbConnection_Test");

        }

        /// <summary>
        /// 跟单猿TMS_运单获取
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostWaybillInfoToWMS([FromBody] WaybillInfoToWMSRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WaybillInfoToWMSResponseDto re = new WaybillInfoToWMSResponseDto();
            WaybillInfoToWMSRequestDto pus = new WaybillInfoToWMSRequestDto();
            RaipiotTokenResponseDto tk = new RaipiotTokenResponseDto();

            pus.createTimeStart = MethodHelp.GetTimestampByDate(DateTime.Today);
            pus.createTimeEnd = DateTimeOffset.Now.ToUnixTimeMilliseconds();

            try
            {
                Dictionary<string, string> urldic = new Dictionary<string, string>(); //查询公用dic
                urldic = new Dictionary<string, string>(); //查询公用dic
                urldic.Add("function_description", "跟单猿_Token获取");//查看URL配置表地址描述
                DataTable url_dt = MethodHelp.GetTableInfo("TMS0004", urldic, con);//获取URL地址
                if (url_dt.Rows.Count > 0)
                {
                    var reAgv2 = DB.httpGet(url_dt.Rows[0]["interface_url"].ToString(), "", "否");
                    RaipiotTokenResponseDto psp = JsonConvert.DeserializeObject<RaipiotTokenResponseDto>(reAgv2);
                    if (psp.status == 200)
                    {
                        urldic = new Dictionary<string, string>(); //查询公用dic
                        urldic.Add("function_description", "跟单猿_运单信息获取");//查看URL配置表地址描述
                        DataTable url_dt2 = MethodHelp.GetTableInfo("TMS0004", urldic, con);//获取URL地址
                        if (url_dt2.Rows.Count>0)
                        {
                            string jsonText = JsonConvert.SerializeObject(pus);
                            var reAgv = DB.httpPost(url_dt2.Rows[0]["interface_url"].ToString(), jsonText, psp.data.token, "是");
                            Ri_WaybillInfoToWMSResponseDto psp2 = JsonConvert.DeserializeObject<Ri_WaybillInfoToWMSResponseDto>(reAgv);
                            if (psp2.status == 200)
                            {
                                Dictionary<string, string> dic = new Dictionary<string, string>(); //查询
                                Dictionary<string, string> dic_in = new Dictionary<string, string>(); //数据传入

                                if (psp2.data!=null)
                                {
                                    foreach (var item in psp2.data)
                                    {
                                        dic = new Dictionary<string, string>();
                                        dic.Add("transportOrderNo", item.transportOrderNo);
                                        DataTable dt_check = MethodHelp.GetTableInfo("TMS0031", dic, con);//查询是否存在运单记录
                                        if (dt_check.Rows.Count <= 0)
                                        {
                                            string loadAddress_start = "";
                                            string deliverCompanyCode_start = "";
                                            string deliverCompanyName_start = "";
                                            string deliverUserName_start = "";
                                            string deliverUserPhone_start = "";
                                            string loadAddress_end = "";
                                            string deliverCompanyCode_end = "";
                                            string deliverCompanyName_end = "";
                                            string deliverUserName_end = "";
                                            string deliverUserPhone_end = "";
                                            string requiredTime = "";
                                            string transportOrderLoadId = "";

                                            foreach (var Load in item.transportTrackLoadResponses)
                                            {
                                                if (Load.loadType == "PLACE_OF_SHIPMENT")
                                                {
                                                    loadAddress_start = Load.loadAddress;
                                                    deliverCompanyCode_start = Load.deliverCompanyCode;
                                                    deliverCompanyName_start = Load.deliverCompanyName;
                                                    deliverUserName_start = Load.deliverUserName;
                                                    deliverUserPhone_start = Load.deliverUserPhone;
                                                    requiredTime = MethodHelp.GetDateTimeMilliseconds(Load.requiredTime).ToShortDateString();
                                                }
                                                else 
                                                {
                                                    loadAddress_end = Load.loadAddress;
                                                    deliverCompanyCode_end = Load.deliverCompanyCode;
                                                    deliverCompanyName_end = Load.deliverCompanyName;
                                                    deliverUserName_end = Load.deliverUserName;
                                                    deliverUserPhone_end = Load.deliverUserPhone;
                                                    transportOrderLoadId = Load.transportOrderLoadId;
                                                }


                                            }
                                            if (item.transportTrackGoodsResponses.Count==0)
                                            {
                                                dic_in = new Dictionary<string, string>(); //数据传入
                                                dic_in.Add("logisticsConfigId", item.logisticsConfigId.ToString());
                                                dic_in.Add("transportOrderNo", item.transportOrderNo.ToString());
                                                dic_in.Add("carrierName", item.carrierName.ToString());
                                                dic_in.Add("createUserNumber", item.createUserNumber.ToString());
                                                dic_in.Add("carrierCode", item.carrierCode.ToString());
                                                dic_in.Add("planeNumber", item.planeNumber.ToString());
                                                dic_in.Add("driverMaName", item.driverMaName.ToString());
                                                dic_in.Add("driverMaPhone", item.driverMaPhone.ToString());
                                                dic_in.Add("orderNo", "");
                                                dic_in.Add("saleNo", "1301");
                                                dic_in.Add("subSaleNo", "");
                                                dic_in.Add("subOrderNo", "1");
                                                dic_in.Add("goodsCode", "null");
                                                dic_in.Add("goodsName", "null");
                                                dic_in.Add("goodsTotal", "1");
                                                dic_in.Add("loadAddress_start", loadAddress_start);
                                                dic_in.Add("requiredTime", requiredTime);
                                                dic_in.Add("deliverCompanyCode_start", deliverCompanyCode_start);
                                                dic_in.Add("deliverCompanyName_start", deliverCompanyName_start);
                                                dic_in.Add("deliverUserName_start", deliverUserName_start);
                                                dic_in.Add("deliverUserPhone_start", deliverUserPhone_start);
                                                dic_in.Add("loadAddress_end", loadAddress_end);
                                                dic_in.Add("deliverCompanyCode_end", deliverCompanyCode_end);
                                                dic_in.Add("deliverCompanyName_end", deliverCompanyName_end);
                                                dic_in.Add("deliverUserName_end", deliverUserName_end);
                                                dic_in.Add("deliverUserPhone_end", deliverUserPhone_end);
                                                dic_in.Add("transportOrderLoadId", transportOrderLoadId);
                                                string strSQL = MethodHelp.GetTranSQL("TMS0030", dic_in, con);
                                                if (strSQL != "")
                                                {
                                                    strSqlList.Add(strSQL.ToString());
                                                }
                                                else
                                                {
                                                    strMsg = "TMS0030 Parameter conversion failed";
                                                }
                                            }
                                            else
                                            {
                                                foreach (var goods in item.transportTrackGoodsResponses)
                                                {
                                                    dic_in = new Dictionary<string, string>(); //数据传入
                                                    dic_in.Add("logisticsConfigId", item.logisticsConfigId.ToString());
                                                    dic_in.Add("transportOrderNo", item.transportOrderNo.ToString());
                                                    dic_in.Add("carrierName", item.carrierName.ToString());
                                                    dic_in.Add("createUserNumber", item.createUserNumber.ToString());
                                                    dic_in.Add("carrierCode", item.carrierCode.ToString());
                                                    dic_in.Add("planeNumber", item.planeNumber.ToString());
                                                    dic_in.Add("driverMaName", item.driverMaName.ToString());
                                                    dic_in.Add("driverMaPhone", item.driverMaPhone.ToString());
                                                    dic_in.Add("orderNo", goods.orderNo.ToString());
                                                    dic_in.Add("saleNo", goods.saleNo.ToString());
                                                    dic_in.Add("subSaleNo", goods.subSaleNo.ToString());
                                                    dic_in.Add("subOrderNo", goods.subOrderNo.ToString());
                                                    dic_in.Add("goodsCode", goods.goodsCode.ToString());
                                                    dic_in.Add("goodsName", goods.goodsName.ToString());
                                                    dic_in.Add("goodsTotal", goods.goodsTotal.ToString());
                                                    dic_in.Add("loadAddress_start", loadAddress_start);
                                                    dic_in.Add("requiredTime", requiredTime);
                                                    dic_in.Add("deliverCompanyCode_start", deliverCompanyCode_start);
                                                    dic_in.Add("deliverCompanyName_start", deliverCompanyName_start);
                                                    dic_in.Add("deliverUserName_start", deliverUserName_start);
                                                    dic_in.Add("deliverUserPhone_start", deliverUserPhone_start);
                                                    dic_in.Add("loadAddress_end", loadAddress_end);
                                                    dic_in.Add("deliverCompanyCode_end", deliverCompanyCode_end);
                                                    dic_in.Add("deliverCompanyName_end", deliverCompanyName_end);
                                                    dic_in.Add("deliverUserName_end", deliverUserName_end);
                                                    dic_in.Add("deliverUserPhone_end", deliverUserPhone_end);
                                                    dic_in.Add("transportOrderLoadId", transportOrderLoadId);
                                                    string strSQL = MethodHelp.GetTranSQL("TMS0030", dic_in, con);
                                                    if (strSQL != "")
                                                    {
                                                        strSqlList.Add(strSQL.ToString());
                                                    }
                                                    else
                                                    {
                                                        strMsg = "TMS0030 Parameter conversion failed";
                                                    }

                                                }
                                            }                                            
                                        }
                                    }
                                    if (strMsg == "")
                                    {
                                        if (strSqlList.Count > 0)
                                        {
                                            bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                                            if (A)
                                            {

                                            }


                                        }
                                    }
                                }                               
                            }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.code = 0;
               
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
